package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.dto.LogExcelDto;
import com.c611.smartclassroom.dto.ScheduleDto;
import com.c611.smartclassroom.excel.MyExcel;
import com.c611.smartclassroom.model.Log;
import com.c611.smartclassroom.model.Schedule;
import com.c611.smartclassroom.service.ScheduleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ScheduleService scheduleService;

    List<ScheduleDto> scheduleDtoList = null;

    String classId = null;

    String semesterId = null;


    /**
     * 通过教室Id和学期ID查询该教室的课表
     * @param classId
     * @return
     */
    @PostMapping("/querySchByClassSeme")
    public DataGridView<ScheduleDto> querySchByClassSeme(@RequestParam("classId") String classId, @RequestParam("semesterId") String semesterId) {
        //1，如果教室id为空，则返回为空
        if(classId == null || semesterId == null) return null;
        if(classId.isEmpty() || semesterId.isEmpty()) return null;
        scheduleDtoList = scheduleService.querySchByClass(classId,semesterId);
        this.classId = classId;
        this.semesterId = semesterId;

        //进行包装
        //1、修改集合中的数据，将\n替换为<br/>
        List<ScheduleDto> datalist = new ArrayList<>();
        for (ScheduleDto scheduleDto : scheduleDtoList) {
            ScheduleDto data = new ScheduleDto();
            data.setLessonName(scheduleDto.getLessonName());
            if(scheduleDto.getMonDay() != null)
                data.setMonDay(scheduleDto.getMonDay().replaceAll("\n","<br/>"));
            if(scheduleDto.getTuesDay() != null)
                data.setTuesDay(scheduleDto.getTuesDay().replaceAll("\n","<br/>"));
            if(scheduleDto.getWednesDay() != null)
                data.setWednesDay(scheduleDto.getWednesDay().replaceAll("\n","<br/>"));
            if(scheduleDto.getThursDay() != null)
                data.setThursDay(scheduleDto.getThursDay().replaceAll("\n","<br/>"));
            if(scheduleDto.getFriDay() != null)
                data.setFriDay(scheduleDto.getFriDay().replaceAll("\n","<br/>"));
            if(scheduleDto.getSaturDay() != null)
                data.setSaturDay(scheduleDto.getSaturDay().replaceAll("\n","<br/>"));
            if(scheduleDto.getSunDay() != null)
                data.setSunDay(scheduleDto.getSunDay().replaceAll("\n","<br/>"));
            datalist.add(data);
        }
        return new DataGridView<>(0,"ok",0L,datalist);
    }

    /**
     * 导出日志
     * @param response
     */
    @RequestMapping("/exportSch")
    public void exportSch(HttpServletResponse response){
        if(scheduleDtoList == null || semesterId == null || classId == null) return;

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = simpleDateFormat.format(date);

        String title =classId + " 教室" + "课表" + "\n" +
                semesterId + " 学期 " +
                "                                                                                                 " +
                "                                                                                                 " +
                 dateStr;

        //导出操作
        MyExcel.exportExcel(scheduleDtoList,title,classId,ScheduleDto.class,classId + "课表.xls",response);
    }

/*    @RequestMapping("/exportSch2")
    public void exportSch2(HttpServletResponse response){

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = simpleDateFormat.format(date);

        String title =classId + " 教室" + "课表" + "\n" +
                semesterId + " 学期 " +
                "                                                                                                 " +
                "                                                                                                 " +
                dateStr;

        //导出操作
        MyExcel.exportExcel(scheduleDtoList,title,classId,ScheduleDto.class,classId + "课表.xls",response);
    }*/

    @RequestMapping("/importSch")
    public int importExcel(@RequestParam("file") MultipartFile file){

        if(semesterId == null || classId == null) return 0;

//        String filePath = "D:\\A000.xls";
        //解析excel，
        List<ScheduleDto> personList = MyExcel.importExcel(file,1,1,ScheduleDto.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
//        System.out.println("导入数据一共【"+personList.size()+"】行");

/*        for (ScheduleDto scheduleDto : personList) {
            System.out.println(scheduleDto);
        }*/

        //TODO 保存数据库

        scheduleService.importSchExcel(personList,classId,semesterId);

//        scheduleDtoList = querySchByClassSeme(classId,semesterId);

        return 1;
    }

}
